﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using SRM.DataObjects;
using SRM;

public partial class UserControls_FamilyDetailsUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["memberName"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["surname"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["nationality"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["relationship"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["birthplace"].DefaultValue)))
                hasFilledAllRequiredFields = false;

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["occupation"].DefaultValue)))
                hasFilledAllRequiredFields = false;
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["familyEmployer"].DefaultValue)))
                hasFilledAllRequiredFields = false; 

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["gender"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["idNumber"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.UpdateParameters["contactNo"].DefaultValue)))
            {
                if (FamilyMembers_ObjectDataSource.UpdateParameters["emergencyContact"].DefaultValue.Equals("YES", StringComparison.InvariantCultureIgnoreCase))
                {
                    hasFilledAllRequiredFields = false; 
                }
            }
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["memberName"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["surname"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["nationality"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["relationship"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["birthplace"].DefaultValue)))
                hasFilledAllRequiredFields = false;

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["occupation"].DefaultValue)))
                hasFilledAllRequiredFields = false;
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["familyEmployer"].DefaultValue)))
                hasFilledAllRequiredFields = false; 

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["gender"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["idNumber"].DefaultValue)))
                hasFilledAllRequiredFields = false; 
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(FamilyMembers_ObjectDataSource.InsertParameters["contactNo"].DefaultValue)))
            {
                if (FamilyMembers_ObjectDataSource.InsertParameters["emergencyContact"].DefaultValue.Equals("YES", StringComparison.InvariantCultureIgnoreCase))
                {
                    hasFilledAllRequiredFields = false; 
                }
            }
                
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgFamily_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            FamilyMembers_ObjectDataSource.UpdateParameters["familyMemberKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("familyMemberKey"));
            FamilyMembers_ObjectDataSource.UpdateParameters["memberName"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("membername_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["relationship"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("relationship_combobox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["birthDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("dateofbirth_datepicker"));
            FamilyMembers_ObjectDataSource.UpdateParameters["nationality"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("nationality_combobox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["familyEmployer"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("familyemployer_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["occupation"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("occupation_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["emergencyContact"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("emergency_combobox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["contactNo"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("contactno_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["surname"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("surname_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["birthplace"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("birthplace_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["gender"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("gender_combobox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["idNumber"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("idnumber_textbox"));
            FamilyMembers_ObjectDataSource.UpdateParameters["residencyType"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("residencytype_radcombobox"));

            if (ValidateFormFields("UPDATE"))
            {
                FamilyMembers_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("FamilyDetailsMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            FamilyMembers_ObjectDataSource.DeleteParameters["familyMemberKey"].DefaultValue = item["FamilyMemberKey_column"].Text;
            FamilyMembers_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            FamilyMembers_ObjectDataSource.InsertParameters["memberName"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("membername_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["relationship"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("relationship_combobox"));
            FamilyMembers_ObjectDataSource.InsertParameters["birthDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("dateofbirth_datepicker"));
            FamilyMembers_ObjectDataSource.InsertParameters["nationality"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("nationality_combobox"));
            FamilyMembers_ObjectDataSource.InsertParameters["familyEmployer"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("familyemployer_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["occupation"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("occupation_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["emergencyContact"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("emergency_combobox"));
            FamilyMembers_ObjectDataSource.InsertParameters["contactNo"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("contactno_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["surname"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("surname_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["birthplace"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("birthplace_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["gender"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("gender_combobox"));
            FamilyMembers_ObjectDataSource.InsertParameters["idNumber"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("idnumber_textbox"));
            FamilyMembers_ObjectDataSource.InsertParameters["residencyType"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("residencytype_radcombobox"));

            if (ValidateFormFields("INSERT"))
            {
                FamilyMembers_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("FamilyDetailsMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }
    }

    protected void rgFamily_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            // Fields in the form template
            System.Web.UI.WebControls.TextBox membername_textbox = null;
            System.Web.UI.WebControls.TextBox surname_textbox = null;
            Telerik.Web.UI.RadComboBox nationality_combobox = null;
            Telerik.Web.UI.RadComboBox relationship_combobox = null;
            System.Web.UI.WebControls.TextBox occupation_textbox = null;
            Telerik.Web.UI.RadDatePicker dateofbirth_datepicker = null;
            System.Web.UI.WebControls.TextBox familyemployer_textbox = null;
            Telerik.Web.UI.RadComboBox emergency_combobox = null;
            System.Web.UI.WebControls.TextBox contactno_textbox = null;
            System.Web.UI.WebControls.TextBox birthplace_textbox = null;
            Telerik.Web.UI.RadComboBox gender_combobox = null;
            System.Web.UI.WebControls.TextBox idnumber_textbox = null;
            Telerik.Web.UI.RadComboBox residencytype_radcombobox = null;

            // Find the controls in the form template
            membername_textbox = geiEditedItem.FindControl("membername_textbox") as System.Web.UI.WebControls.TextBox;
            surname_textbox = geiEditedItem.FindControl("surname_textbox") as System.Web.UI.WebControls.TextBox;
            nationality_combobox = geiEditedItem.FindControl("nationality_combobox") as Telerik.Web.UI.RadComboBox;
            relationship_combobox = geiEditedItem.FindControl("relationship_combobox") as Telerik.Web.UI.RadComboBox;
            occupation_textbox = geiEditedItem.FindControl("occupation_textbox") as System.Web.UI.WebControls.TextBox;
            dateofbirth_datepicker = geiEditedItem.FindControl("dateofbirth_datepicker") as Telerik.Web.UI.RadDatePicker;
            familyemployer_textbox = geiEditedItem.FindControl("familyemployer_textbox") as System.Web.UI.WebControls.TextBox;
            emergency_combobox = geiEditedItem.FindControl("emergency_combobox") as Telerik.Web.UI.RadComboBox;
            contactno_textbox = geiEditedItem.FindControl("contactno_textbox") as System.Web.UI.WebControls.TextBox;
            birthplace_textbox = geiEditedItem.FindControl("birthplace_textbox") as System.Web.UI.WebControls.TextBox;
            gender_combobox = geiEditedItem.FindControl("gender_combobox") as Telerik.Web.UI.RadComboBox;
            idnumber_textbox = geiEditedItem.FindControl("idnumber_textbox") as System.Web.UI.WebControls.TextBox;
            residencytype_radcombobox = geiEditedItem.FindControl("residencytype_radcombobox") as Telerik.Web.UI.RadComboBox;

            // Databind the 3 combo-boxes (nationality_combobox, relationship_combobox, residencytype_radcombobox)
            Help.BindSapCodeToComboBox(nationality_combobox, SRM_Code.GetNationality("1"));
            Help.BindSapCodeToComboBox(relationship_combobox, SRM_Code.GetRelationship("1"));
            Help.BindSapCodeToComboBox(residencytype_radcombobox, SRM_Code.GetResidencyType("1"));

            // Assign values if editing existing record
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                DataRow row = ((DataRowView)e.Item.DataItem).Row;
                membername_textbox.Text = row["MemberName"].ToString();
                surname_textbox.Text = row["Surname"].ToString();
                occupation_textbox.Text = row["Occupation"].ToString();

                //nationality_combobox.SelectedValue = row["Nationality"].ToString();
                //relationship_combobox.SelectedValue = row["Relationship"].ToString();
                Help.SetComboBoxSelectedValue(nationality_combobox, row.Field<string>("Nationality"));
                Help.SetComboBoxSelectedValue(relationship_combobox, row.Field<string>("Relationship"));

                

                dateofbirth_datepicker.SelectedDate = row["BirthDate"] as DateTime?;
                familyemployer_textbox.Text = row["FamilyEmployer"].ToString();
                contactno_textbox.Text = row["ContactNo"].ToString();
                birthplace_textbox.Text = row["BirthPlace"].ToString();
                idnumber_textbox.Text = row["IdNumber"].ToString();
                //emergency_combobox.SelectedValue = row["EmergencyContact"].ToString();
                //gender_combobox.SelectedValue = row["Gender"].ToString();
                //residencytype_radcombobox.SelectedValue = row["ResidencyType"].ToString();
                Help.SetComboBoxSelectedValue(emergency_combobox, row.Field<string>("EmergencyContact"));
                Help.SetComboBoxSelectedValue(gender_combobox, row.Field<string>("Gender"));
                Help.SetComboBoxSelectedValue(residencytype_radcombobox, row.Field<string>("ResidencyType"));
            }


        } // if (e.Item is GridEditFormItem && e.Item.IsInEditMode)

    } // protected void rgFamily_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
}